package in.muton.edushare.pages.deptpages;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.components.images.ProfileImage;
import in.muton.edushare.helper.TinyMCEHelper;
import in.muton.edushare.panels.upload.single.SingleUploadButtonPanel;
import in.muton.edushare.url.EntityPageParams;

import java.util.Arrays;
import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.dao.FileDirectoriesDAO;
import com.edushare.common.file.image.ProfileImages;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.FileDirectories;
import com.edushare.common.vo.FilesUploaded;
import com.edushare.common.vo.UserDeptLink;

public class DeptEdit extends DeptBase {

	public DeptEdit(PageParameters params) {
		super(params);
		prc.check(Arrays.asList(UserDeptLink.PRIMARY_ADMIN));
		Form<Void> f = new Form<Void>("form");
		add(f);
		f.add(new TextField<String>("name", new PropertyModel<String>(super
				.getModel(), "deptName")));
		f.add(new TextArea<String>("desc", new PropertyModel<String>(super
				.getModel(), "deptDesc")).add(TinyMCEHelper
				.getDefaultTinyMCEBehavior()));
		final ProfileImage pI = new ProfileImage("img", super.getModel(),
				ApplicationConstants.FILE_SUFFIX_HBADGE);
		pI.setOutputMarkupId(true);
		f.add(pI);
		SingleUploadButtonPanel uploadButton = new SingleUploadButtonPanel

		("imgUpload", new PropertyModel<FileDirectories>(DeptEdit.this,
				"deptGallery")) {
			private static final long serialVersionUID = 6677445289509418613L;

			@Override
			protected void onUploadWindowClose(AjaxRequestTarget target) {
				List<FilesUploaded> lf = EduShareSession.get()
						.getFileUploadedInLastUpload();
				if (lf != null && lf.size() > 0) {
					FilesUploaded fu = lf.get(lf.size() - 1);// last item
																// uploaded
					if (fu != null) {
						getModel().getObject().setImageFile(fu);
						getSessionORMInterface().commitAllChanges();
					}
				}
				target.addComponent(pI);
			}
		};
		f.add(uploadButton);
		f.add(new Button("submit") {

			private static final long serialVersionUID = -8273424307820510119L;

			@Override
			public void onSubmit() {
				Departments dept = DeptEdit.this.getModel().getObject();
				if (!StringUtil.isValid(dept.getDeptName())) {
					setErrorMessage("Department name is required");
					return;
				}
				clearError();
				getSessionORMInterface().commitAllChanges();
				setResponsePage(DeptPage.class, EntityPageParams
						.getParams(DeptEdit.this.getModel()));
			}
		});

		add(f);
		//
	}

	@Override
	protected String getActivePage(PageParameters params) {
		return "deptProfile";
	}

	@Override
	public String getPageTitle() {
		return "Edit Department Details | "
				+ getModel().getObject().getDeptName() + " | "
				+ ApplicationConstants.APPLICATION_NAME;
	}

	public FileDirectories getDeptGallery() {
		FileDirectories fd = new FileDirectoriesDAO(getSessionORMInterface())
				.getOrCreateDefaultGallery(getModel().getObject());
		fd.setFileProcessStrategy(new ProfileImages());
		return fd;
	}

}
